package com.qfjy.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @ClassName RedisConfig
 * @Description TODO
 * @Author guoweixin
 * @Date 2022/3/23
 * @Version 1.0
 */
@Configuration
public class RedisConfig {


    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);

        //String序列化配置
        StringRedisSerializer stringRedisSerializer=new StringRedisSerializer();
        //JSON序列化配置
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer=new GenericJackson2JsonRedisSerializer();

        //Redis key
        template.setKeySerializer(stringRedisSerializer);
        //Redis string  value
        template.setValueSerializer(genericJackson2JsonRedisSerializer);


        // Hash key 和Hash Value (稍候）
        template.setHashKeySerializer(stringRedisSerializer);
        //Hash value序列化（JSON）
        template.setHashValueSerializer(genericJackson2JsonRedisSerializer);

        return template;
    }

}
